package com.example.websocket.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.websocket.Controller.R;
import com.example.websocket.DAO.RelationDAO;
import com.example.websocket.DAO.UserDAO;
import com.example.websocket.Service.UserService;
import com.example.websocket.pojo.Relation;
import com.example.websocket.pojo.Result;
import com.example.websocket.pojo.User;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDAO userDAO;

    @Autowired
    private RelationDAO relationDAO;

    @Override
    public Result login(String username, String pwd) {
        Result result = new Result();
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
        lqw.eq(Strings.isNotEmpty(username),User::getName,username);
        User user =userDAO.selectOne(lqw);
        if (pwd.equals(user.getPassword())){
            result.setFlag(true);
        }else {
            result.setFlag(false);
        }
        return result;
    }

    @Override
    public List<User> GetFriend(String username) {
        //根据用户名查询出用户id
        List<User> userList = new ArrayList<>();
        LambdaQueryWrapper<User> SelectUserByName = new LambdaQueryWrapper<User>();
        SelectUserByName.eq(Strings.isNotEmpty(username),User::getName,username);
        User user = userDAO.selectOne(SelectUserByName);
        LambdaQueryWrapper<Relation> SelectFriendid = new LambdaQueryWrapper<Relation>();
        SelectFriendid.eq(user.getId()!=null,Relation::getUserid,user.getId());
        List<Relation> list = relationDAO.selectList(SelectFriendid);
        for (Relation relation : list) {
            User friend = userDAO.selectById(relation.getFriendid());
            userList.add(friend);
        }
        return userList;
    }

    @Override
    public R AddFriend(String username,String fname) {
        //查询条件为用户名称
        LambdaQueryWrapper<User> SelectFUserByName = new LambdaQueryWrapper<User>();
        SelectFUserByName.eq(Strings.isNotEmpty(fname),User::getName,fname);
        User Fuser = userDAO.selectOne(SelectFUserByName);
        //若查询不出用户
        if(Fuser==null){
            return new R(false);
        }else {
            LambdaQueryWrapper<User> SelectMUserByName = new LambdaQueryWrapper<User>();
            SelectMUserByName.eq(Strings.isNotEmpty(username),User::getName,username);
            User Muser = userDAO.selectOne(SelectMUserByName);
            relationDAO.insert(new Relation(null,Muser.getId(),Fuser.getId()));
        }
        return new  R(true);
    }

    @Override
    public R DelFriend(String username, String dname) {
        //查询条件为用户名称
        LambdaQueryWrapper<User> SelectDUserByName = new LambdaQueryWrapper<User>();
        SelectDUserByName.eq(Strings.isNotEmpty(dname),User::getName,dname);
        User Duser = userDAO.selectOne(SelectDUserByName);
        //若查询不出用户
        if(Duser==null){
            return new R(false);
        }
        LambdaQueryWrapper<User> SelectMUserByName = new LambdaQueryWrapper<User>();
        SelectMUserByName.eq(Strings.isNotEmpty(username),User::getName,username);
        User Muser = userDAO.selectOne(SelectMUserByName);
        LambdaQueryWrapper<Relation> DeleteRelation = new LambdaQueryWrapper<Relation>();
        DeleteRelation.eq(Relation::getUserid,Muser.getId());
        DeleteRelation.eq(Relation::getFriendid,Duser.getId());
        int result = relationDAO.delete(DeleteRelation);
        if (result==0){
            return new R(false);
        }
        return new  R(true);
    }
}
